package clue.engine;
import java.util.ArrayList;
import java.util.Collections;

import clue.cards.Card;
import clue.cards.RoomCard;
import clue.cards.SuspectCard;
import clue.cards.WeaponCard;
import clue.enums.Room;
import clue.enums.Suspect;
import clue.enums.Weapon;

/**
 *
 * @author Shawn
 */
public final class Deck {
  private ArrayList<RoomCard> roomCardPile;
  private ArrayList<SuspectCard> suspectCardPile;
  private ArrayList<WeaponCard> weaponCardPile;
  private ArrayList<Card> mixedPile;

  public Deck() {
    roomCardPile = new ArrayList<RoomCard>();
    for (Room r : Room.values())
      roomCardPile.add(new RoomCard(r));

    suspectCardPile = new ArrayList<SuspectCard>();
    for (Suspect s : Suspect.values())
      suspectCardPile.add(new SuspectCard(s));

    weaponCardPile = new ArrayList<WeaponCard>();
    for (Weapon w : Weapon.values())
      weaponCardPile.add(new WeaponCard(w));

    mixedPile = new ArrayList<Card>();

    shuffle();
  }

  void shuffleMixedCards() {
    Collections.shuffle(mixedPile);
  }

  void shuffleRoomCards() {
    Collections.shuffle(roomCardPile);
  }

  void shuffleSuspectCards() {
    Collections.shuffle(suspectCardPile);
  }

  void shuffleWeaponCards() {
    Collections.shuffle(weaponCardPile);
  }

  void shuffle() {
    shuffleMixedCards();
    shuffleRoomCards();
    shuffleSuspectCards();
    shuffleWeaponCards();
  }

  RoomCard removeRoomCard() {
    if (roomCardPile.isEmpty())
      return null;
    return roomCardPile.remove(roomCardPile.size() - 1);
  }

  SuspectCard removeSuspectCard() {
    if (suspectCardPile.isEmpty())
      return null;
    return suspectCardPile.remove(suspectCardPile.size() - 1);
  }

  WeaponCard removeWeaponCard() {
    if (weaponCardPile.isEmpty())
      return null;
    return weaponCardPile.remove(weaponCardPile.size() - 1);
  }

  Card removeMixedCard() {
    if (mixedPile.isEmpty())
      return null;
    return mixedPile.remove(mixedPile.size() - 1);
  }

  void mixCards() {
    while (!roomCardPile.isEmpty())
      mixedPile.add(removeRoomCard());
    while (!suspectCardPile.isEmpty())
      mixedPile.add(removeSuspectCard());
    while (!weaponCardPile.isEmpty())
      mixedPile.add(removeWeaponCard());
    shuffleMixedCards();
  }

  void separateCards() {
    while (!mixedPile.isEmpty()) {
      Card c = removeMixedCard();
      if (c instanceof RoomCard)
        roomCardPile.add((RoomCard) c);
      else if (c instanceof SuspectCard)
        suspectCardPile.add((SuspectCard) c);
      else if (c instanceof WeaponCard)
        weaponCardPile.add((WeaponCard) c);
      else
        assert(false);
    }
    
    shuffle();
  }

  boolean isMixedPileEmpty() {
	  return mixedPile.isEmpty();
  }
}
